<template>
  <div class="container-big wing-blank dingtalk-my-ticket-page">
    <!-- <img src="https://img.alicdn.com/tfs/TB15gSFIHvpK1RjSZFqXXcXUVXa-200-50.png" class="ucan-ticket-logo" alt=""> -->
    <!-- <div class="uncan-user-avatar">
      <img :src="user.avatar && user.avatar.indexOf('http') !== -1 ? user.avatar : defaultAvatar ">
      <div v-if="faceAuthed" class="uncan-user-avatar-badge">
        已录入人脸
      </div>
    </div>
    <p class="ucan-user-name">
      {{user.name}}
    </p> -->

    <h3 class="my-ticket-title">
      我的门票
    </h3>
    <ucan-ticket @click="goDetail(ticket)" v-for="(ticket, index) in tickets" :key="index" :id="ticket.ticketId" :name="ticket.uName" :type="ticket.tTypeName" :address="ticket.address" :price="ticket.price" :forwarded="ticket.isActive === 5" :expired="ticket.isActive === 3" :used="ticket.isActive === 4" :activityId="ticket.activityId" :appointment="!schedules || schedules.length === 0" :activeTime="{stime: ticket.stime, etime: ticket.etime, activeDate: ticket.activeDate}" />
    <my-schedule v-if="tickets && tickets.length && schedules && schedules.length > 0" :list="schedules" />
    <co-dialog :open="open" class="ticket-auth-dialog-wrapper" dialog-class="ticket-auth-dialog">
      <div class="ticket-auth-dialog-header">
        <h3 class="ticket-auth-dialog-title">人脸快速入场</h3>
        <co-icon-close class="ticket-auth-dialog-close" @click="closeFaceAuth" />
        <img src="https://gw.alicdn.com/tfs/TB1qKkQHirpK1RjSZFhXXXSdXXa-698-696.png" class="ticket-auth-avatar">
      </div>
      <div class="ticket-auth-dialog-content">
        <p>大会要求实名制人脸识别核验入场，需要您配合录入面部特征并授权，主办方用于现场核验。</p>
        <p class="argeement-desc">我已授权并同意<a target="_blank" :href="agreement">《人脸信息授权协议》</a></p>
      </div>
      <div class="ticket-auth-dialog-action" @click="photo">同意并继续</div>
      <van-loading color="white" v-if="uploading" class="co-loading" />
    </co-dialog>
  </div>
</template>
<script>
import dd from "dingtalk-jsapi";
import UcanTicket from "./ucan-ticket";
import MySchedule from "./schedule";
import services from "../../api/mine";
import user from "./user";
import { getURLParameter } from "../../utils";

export default {
  data() {
    return {
      agreement: `https://ucan-ticket.alibabadesign.com/protocol/?corpId=${sessionStorage.getItem(
        "corpId"
      )}&agereeId=${3}#/`,
      user: {},
      tickets: [],
      schedules: [],
      open: false,
      uploading: false,
      faceAuthed: false,
      defaultAvatar:
        "https://img.alicdn.com/tfs/TB1qTpgIxnaK1RjSZFtXXbC2VXa-340-340.png",
      activityId: Number(getURLParameter("activityId"))
    };
  },
  async mounted() {
    this.user = user;

    // this.checkFaceAuth();

    const [tickets, schedules] = await Promise.all([
      services.getTicketList(),
      services.getBranchMeet()
    ]);

    this.tickets = tickets;
    this.schedules = schedules;
  },
  methods: {
    closeFaceAuth() {
      this.open = false;
    },
    checkFaceAuth() {
      return services
        .isNeedUploadFace(this.activityId)
        .then(async ({ hasEnterFace, review }) => {
          this.faceAuthed = hasEnterFace && review;
          if (!hasEnterFace) {
            this.open = true;
          } else if (!review) {
            const result = await this.$confirm(
              "很抱歉！您录入的人脸信息审核失败了。请重新录入您的人脸信息，以免影响您使用人脸快速入场通道",
              {
                title: "人脸信息审核失败",
                okLabel: "重新录入",
                cancelLabel: "放弃",
                showCancel: true
              }
            );
            this.open = result;
          }
        });
    },
    goDetail(ticket) {
      this.$router.push({
        path: "/mine/ticket",
        query: {
          ticketId: ticket.ticketId,
          ticketTypeId: ticket.ticketTypeId,
          activityId: ticket.activityId
        }
      });
    },
    photo() {
      if (this.uploading) return;

      dd.biz.util.uploadImageFromCamera({
        compression: true,
        onSuccess: ([imgURL]) => {
          this.uploading = true;
          services
            .uploadFace(this.activityId, imgURL)
            .then(() => {
              this.$toast("认证成功");
              this.closeFaceAuth();
            })
            .catch(reason => this.$toast(reason))
            .then(() => {
              this.uploading = false;
            });
        },
        onFail: error => {
          this.$toast("拍照失败");
        }
      });
    }
  },
  components: {
    UcanTicket,
    MySchedule
  }
};
</script>
<style lang="less">
.dingtalk-my-ticket-page {
  padding-top: 56px;
  padding-bottom: 30px;
  .ucan-ticket-logo {
    width: 201px;
    display: block;
  }

  .uncan-user-avatar {
    width: 170px;
    height: 170px;
    margin: 46px auto 36px;
    position: relative;
    > img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
  .uncan-user-avatar-badge {
    padding: 0 18px;
    height: 38px;
    font-size: 20px;
    line-height: 38px;
    text-align: center;
    background-image: linear-gradient(116deg, #ff6325 0%, #ed006f 100%);
    border-top-right-radius: 19px;
    border-bottom-right-radius: 19px;
    border-bottom-left-radius: 19px;
    white-space: nowrap;
    position: absolute;
    right: -80px;
    bottom: 0;
  }

  .ucan-user-name {
    font-size: 34px;
    text-align: center;
    margin-bottom: 44px;
  }

  .uncan-ticket-authentications {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }

  .uncan-ticket-auth {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex: 1;
    height: 106px;
    border-radius: 4px;
    background-color: rgba(0, 0, 0, 0.5);
    padding: 16px 0px 16px 24px;
    margin-right: 16px;
    &:last-child {
      margin-right: 0;
    }
  }

  .uncan-ticket-auth-icon {
    width: 48px;
    height: 48px;
    margin-right: 24px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .uncan-ticket-auth-title {
    font-size: 28px;
    color: #fff;
    text-align: left;
  }
  .uncan-ticket-auth-desc {
    font-size: 20px;
    text-align: left;
    color: rgba(238, 238, 238, 0.6);
    white-space: nowrap;
    margin-top: 6px;
  }

  .my-ticket-title {
    font-size: 32px;
    color: #eeeeee;
    text-align: left;
    margin-top: 54px;
    margin-bottom: 24px;
  }

  .uncan-my-schedule {
    margin-top: 48px;
  }
}

.ticket-auth-dialog {
  width: 638px;
  border-radius: 4px;
  position: relative;
  .co-dialog-body {
    padding: 0;
  }

  .ticket-auth-avatar {
    width: 347px;
    display: block;
    margin: 10px auto 0;
  }
}

.ticket-auth-dialog-header {
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
  height: 530px;
  background: url(https://gw.alicdn.com/tfs/TB1XIkLHgTqK1RjSZPhXXXfOFXa-638-515.png)
    no-repeat;
  background-size: cover;
  width: 100%;
}

.ticket-auth-dialog-title {
  padding: 32px 0;
  text-align: center;
  font-size: 36px;
  color: #fff;
  font-weight: normal;
}

.ticket-auth-dialog-close {
  position: absolute;
  right: 24px;
  top: 24px;
  color: #fff;
}

.ticket-auth-dialog-content {
  padding: 32px;
  color: #333;
  font-size: 28px;
  line-height: 1.6;
  .argeement-desc {
    color: #888;
    margin-top: 24px;
    > a {
      color: #108ee9;
    }
  }
}

.ticket-auth-dialog-action {
  height: 102px;
  line-height: 101px;
  border-top: 1px solid #e5e5e5;
  color: #108ee9;
  font-size: 36px;
  display: block;
  width: 100%;
  text-align: center;
}
</style>

